package com.swpu.cbg.cnshop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.activity.WareDetilActivity;
import com.swpu.cbg.cnshop.adapter.BaseAdapter;
import com.swpu.cbg.cnshop.adapter.DividerItemDecortion;
import com.swpu.cbg.cnshop.adapter.HWAdapter;
import com.swpu.cbg.cnshop.adapter.HotAdapter;
import com.swpu.cbg.cnshop.bean.PageContent;
import com.swpu.cbg.cnshop.bean.Ware;
import com.swpu.cbg.cnshop.http.Pager;
import com.swpu.cbg.cnshop.utils.CartProvider;

import java.util.List;

/**
 * Created by chenboge on 16/1/30.
 */
public class HotFragment extends BaseFragment {


    private View mView;
    private LayoutInflater mInflater;
    private List<Ware> mWares;
    private HotAdapter mAdapter;
    private HWAdapter<Ware> mHAdapter;
    private CartProvider mProvider;


    @ViewInject(R.id.id_hot_recyclerview)
    private RecyclerView mRecycleview;

    @ViewInject(R.id.id_hot_refresh)
    private MaterialRefreshLayout mMaterialRefreshLayout;


    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mInflater = LayoutInflater.from(getActivity());
        mView = mInflater.inflate(R.layout.hot, container, false);
        return mView;
    }

    @Override
    public void init() {
        initRecyData();
    }

    private void initRecyData() {
        Pager pager = Pager.newBuilder().setCurPage(1).setPageSize(12).setUrl(Constans.API.HOT_CONTENT).setLoadMore(true).
                setmMaterialRefreshLayout(mMaterialRefreshLayout).setOnPagerListener(new Pager.OnPagerListener() {
            @Override
            public void load(List data, int totlePage, int totleCount) {
                mWares = data;
                method_normal();
            }

            @Override
            public void loadMore(List data, int totlePage, int totleCount) {
                mWares = data;
                method_loadmore();
            }

            @Override
            public void refresh(List data, int totlePage, int totleCount) {
                mWares = data;
                method_refresh();
            }
        }).builder(getActivity(), new TypeToken<PageContent<Ware>>() {
        }.getType());

        pager.request();
    }


    private void method_refresh() {
        mHAdapter.ClearData();
        mHAdapter.AddData(mWares);
        mRecycleview.scrollToPosition(0);
        mMaterialRefreshLayout.finishRefresh();
    }

    private void method_loadmore() {
        int itemSize = mHAdapter.getDatas().size();
        mHAdapter.AddData(itemSize, mWares);
        mRecycleview.scrollToPosition(mHAdapter.getDatas().size());
        mMaterialRefreshLayout.finishRefreshLoadMore();
    }

    private void method_normal() {
        mHAdapter = new HWAdapter<Ware>(getActivity(), mWares, R.layout.template_hot_recycleview);
        mHAdapter.setOnItemClickListener(new BaseAdapter.onItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), WareDetilActivity.class);
                intent.putExtra(Constans.API.TAG_WARE, mWares.get(position));
                startActivity(intent);
            }
        });

        mHAdapter.setOnItemButtonClickListener(new HWAdapter.OnItemButtonClickListener() {
            @Override
            public void OnClick(View view, Ware ware) {
                mProvider = new CartProvider(getActivity());
                mProvider.put(ware);
                Toast.makeText(getActivity(), "添加" + ware.getName() + "成功", Toast.LENGTH_SHORT).show();
            }
        });
//        mAdapter.setOnItemButtonClickListener(new HotAdapter.OnItemButtonClickListener() {
//            @Override
//            public void OnClick(View view, Ware ware) {
//                mProvider = new CartProvider(getActivity());
//                mProvider.put(ware);
//                Toast.makeText(getActivity(), "添加" + ware.getName() + "成功", Toast.LENGTH_SHORT).show();
//            }
//        });
        mRecycleview.setAdapter(mHAdapter);
        mRecycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecycleview.addItemDecoration(new DividerItemDecortion());
    }


}
